﻿/**
* QSmart Andon
*
* 2012-10-23
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Service.BasicDataManage;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.Business.BasicDataManage
{
    public class LevelBusiness
    {
        private readonly LevelService _dal = new LevelService();

        private readonly LevelDetailService _detaildal = new LevelDetailService();

        public string GetLevelList(LevelQcEntity qcmodel)
        {
            var total = _dal.GetLevelCount(qcmodel);
            var list = _dal.GetLevelList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public string GetLevelInfo(string lvId)
        {
            return JsonHelper.Encode(_dal.GetLevelInfo(lvId));
        }

        public int GetMaxLevelIdx(LevelQcEntity qcmodel)
        {
            var result = _dal.GetMaxLevelIdx(qcmodel);
            if (result >= 0)
            {
                result++;
            }
            return result;
        }

        public bool AddLevel(string data)
        {
            var model = new LevelEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<LevelDetailEntity>();
            foreach (Hashtable row in rows)
            {
                model.LvID = Guid.NewGuid();
                model.LineID = new Guid(row["LineID"].ToString());
                model.ETypeCode = row["ETypeCode"].ToString();
                model.LevelIdx = Convert.ToInt32(row["LevelIdx"].ToString());
                model.MsgTimeOut = Convert.ToInt32(row["MsgTimeOut"].ToString());
                model.TimeoutStatus = Convert.ToInt32(row["TimeoutStatus"].ToString());
                model.ArrList = (ArrayList)row["arrList"];
                list.AddRange(from Hashtable item in model.ArrList
                              select new LevelDetailEntity
                                         {
                                             RowID = Guid.NewGuid(),
                                             LvID = model.LvID,
                                             UserID = new Guid(item["UserID"].ToString())
                                         });
            }
            _detaildal.AddLevelDetail(list);
            return _dal.AddLevel(model);
        }

        public bool ModifyLevel(string data)
        {
            var model = new LevelEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<LevelDetailEntity>();
            foreach (Hashtable row in rows)
            {
                model.LvID = new Guid(row["LvID"].ToString());
                model.MsgTimeOut = Convert.ToInt32(row["MsgTimeOut"].ToString());
                model.TimeoutStatus = Convert.ToInt32(row["TimeoutStatus"].ToString());
                model.ArrList = (ArrayList)row["arrList"];
                list.AddRange(from Hashtable item in model.ArrList
                              select new LevelDetailEntity
                                         {
                                             RowID = Guid.NewGuid(),
                                             LvID = model.LvID,
                                             UserID = new Guid(item["UserID"].ToString())
                                         });
            }
            _detaildal.RemoveLevelDetail(model.LvID.ToString());

            _detaildal.AddLevelDetail(list);

            return _dal.ModifyLevel(model);
        }

        public int GetMaxLevelIdxs(LevelQcEntity qcmodel)
        {
            return _dal.GetMaxLevelIdx(qcmodel);
        }

        public bool RemoveLevel(string id)
        {
            _detaildal.RemoveLevelDetail(id);
            return _dal.RemoveLevel(id);
        }
    }
}
